package com.hzya.frame.system.role.service.impl;

import com.hzya.frame.system.module.dao.IModuleDao;
import com.hzya.frame.system.module.entity.ModuleEntity;
import com.hzya.frame.system.role.dao.IRoleDao;
import com.hzya.frame.system.role.dao.IRoleModuleDao;
import com.hzya.frame.system.role.entity.RoleEntity;
import com.hzya.frame.system.role.entity.RoleModuleEntity;
import com.hzya.frame.system.role.service.IRoleService;
import com.hzya.frame.system.user.dao.IUserRoleDao;
import com.hzya.frame.system.user.entity.UserEntity;
import com.hzya.frame.system.user.entity.UserRoleEntity;
import com.hzya.frame.utils.core.action.SessionContext;
import com.hzya.frame.utils.core.service.BaseService;
import com.hzya.frame.utils.core.tree.TreeNode;
import com.hzya.frame.utils.help.TreeNodeHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * @description: 角色  service
 * @tableName:   system_role 
 * @entityName:  RoleEntity
 * @author: 	 gjh
 * @history:	 1.0
 */
@Service(value="system_roleService")
public class  RoleServiceImpl extends BaseService< RoleEntity,Long> implements IRoleService {
	
	protected IRoleDao roleDao;
	@Autowired
	private IRoleModuleDao roleModuleDao;
	@Autowired
	private IModuleDao moduleDao;
	@Autowired
	private IUserRoleDao userRoleDao;
	@Autowired
	public void setRoleDao(IRoleDao dao) {

		this.roleDao = dao;
		this.dao=dao;
	}
	@Override
	/**
	 * 获得角色模块树形结构
	 *
	 * @param roleDomain
	 * @return
	 * @throws Exception
	 */
	public List<TreeNode> getRoleModuleTree(RoleEntity roleEntity) throws Exception {
		RoleModuleEntity roleModuleEntity = new RoleModuleEntity();
		if (null != roleEntity)
			roleModuleEntity.setRole_id(roleEntity.getId());
		List<RoleModuleEntity> roleModuleList = roleModuleDao.query(roleModuleEntity);
		List<TreeNode> resultList = new ArrayList<TreeNode>();
		List<ModuleEntity> allModuleList = moduleDao.getAllExp();
		TreeMap<Long, TreeNode> treeMap = new TreeMap<Long, TreeNode>();
		for (ModuleEntity tempDomain : allModuleList) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(tempDomain.getId());
			treeNode.setText(tempDomain.getName());
			treeNode.setParentId(tempDomain.getUp_id());
			treeNode.setState("closed");
			for (RoleModuleEntity tempSystemRoleModuleDomain : roleModuleList) {
				if (!tempDomain.getId().equals(0L) && tempDomain.getId() != null
						&& tempSystemRoleModuleDomain.getId() != null
						&& tempSystemRoleModuleDomain.getModule_id().equals(tempDomain.getId())) {
					// tempDomain.getName());
					treeNode.setChecked(true);
				}
			}
			treeMap.put(tempDomain.getId(), treeNode);
		}
		resultList.add(TreeNodeHelper.buildTree(treeMap.get(-1L), treeMap));

		return resultList;
	}

	/**
	 * 覆盖父类的save方法
	 */
	@Override
	public RoleEntity save(RoleEntity roleEntity) throws Exception {
		roleDao.save(roleEntity);
		if (!StringUtils.isEmpty(roleEntity.getModule_ids()) && !StringUtils.isEmpty(roleEntity.getModule_names())) {
			String[] module_ids = roleEntity.getModule_ids().split(",");
			String[] module_names = roleEntity.getModule_names().split(",");
			saveRoleModule(roleEntity, module_ids);
		}

		return roleEntity;
	}

	/**
	 * 覆盖父类的update方法
	 */
	@Override
	public RoleEntity update(RoleEntity roleEntity) throws Exception {
		roleDao.update(roleEntity);
		roleModuleDao.deleteByRole(roleEntity);
		if (!StringUtils.isEmpty(roleEntity.getModule_ids()) && !StringUtils.isEmpty(roleEntity.getModule_names())) {
			String[] module_ids = roleEntity.getModule_ids().split(",");
			String[] module_names = roleEntity.getModule_names().split(",");
			saveRoleModule(roleEntity, module_ids);
		}
		return roleEntity;
	}

	private void saveRoleModule(RoleEntity roleEntity, String[] module_ids) {
		for (int i = 0; i < module_ids.length; i++) {
            RoleModuleEntity roleModuleDomain = new RoleModuleEntity();
            roleModuleDomain.setRole_id(roleEntity.getId());
            roleModuleDomain.setModule_id(Long.valueOf(module_ids[i]));
            roleModuleDao.save(roleModuleDomain);
        }
	}

	@Override
	public List<TreeNode> getRoleModuleTreeForShow(RoleEntity roleEntity) throws Exception {
		RoleModuleEntity roleModuleEntity = new RoleModuleEntity();
		if (null != roleEntity)
			roleModuleEntity.setRole_id(roleEntity.getId());
		List<RoleModuleEntity> roleModuleList = roleModuleDao.query(roleModuleEntity);
		List<TreeNode> resultList = new ArrayList<TreeNode>();
		List<ModuleEntity> allModuleList = moduleDao.getAllExp();
		TreeMap<Long, TreeNode> treeMap = new TreeMap<Long, TreeNode>();
		for (ModuleEntity tempDomain : allModuleList) {
			TreeNode treeNode = new TreeNode();
			treeNode.setId(tempDomain.getId());
			treeNode.setText(tempDomain.getName());
			treeNode.setParentId(tempDomain.getUp_id());
			treeNode.setState("closed");
			for (RoleModuleEntity tempSystemRoleModuleDomain : roleModuleList) {
				if (!tempDomain.getId().equals(0L) && tempDomain.getId() != null
						&& tempSystemRoleModuleDomain.getId() != null
						&& tempSystemRoleModuleDomain.getModule_id().equals(tempDomain.getId())) {
					// tempDomain.getName());
					treeMap.put(tempDomain.getId(), treeNode);
				}
			}
		}
		if (!treeMap.isEmpty())
			resultList.add(TreeNodeHelper.buildTree(treeMap.get(-1L), treeMap));
		return resultList;
	}

	/**
	 * 获取当前登陆人所在公司的所有的角色
	 * @return
	 */
	@Override
	public List<RoleEntity> listGetCompanyRoles( ) {

 		List<RoleEntity> result = null;
		long company_org_id = SessionContext.getCurrentUser().getCompany_org_id();
		/** 判断是不是超级管理员*/
		if(company_org_id < 0){
			result= roleDao.getAll();
		}else{
			RoleEntity roleEntity = new RoleEntity();
			roleEntity.setCompany_org_id(company_org_id);
			roleDao.query(roleEntity);
		}

		return result;
	}


}
